"""Working with database"""

import sqlite3
from uuid import uuid4

from parsers.pdf import get_pdf_content


def get_books(db: str) -> list[str]:
    """Get all books from database"""
    with sqlite3.connect(db) as conn:
        cursor = conn.cursor()
        cursor.execute('SELECT id, title FROM books')
        books = cursor.fetchall()
        return books


if __name__ == '__main__':
    with sqlite3.connect('books.db') as conn:
        try:
            # todo add sentence position to contents
            c = conn.cursor()
            c.execute("CREATE TABLE IF NOT EXISTS books(id, title)")
            c.execute("CREATE TABLE IF NOT EXISTS contents(id, id_book, num, text)")

            book_id = uuid4().hex
            book_name = 'test.pdf'
            c.execute("INSERT INTO books VALUES (?,?)", (book_id, book_name))
            sents = get_pdf_content(book_name)
            for i, sent in enumerate(sents):
                content_id = uuid4().hex
                c.execute("INSERT INTO contents VALUES (?,?,?,?)", (content_id, book_id, i, sent))
        finally:
            c.close()
